﻿using System;
using UniMana.Utils.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using UniMana.Utils.Controls;
using UniMana.Data;
using UniMana.Utils.Helper;

namespace UniMana.Win.Training
{
    public partial class FrmTrainingProgram : DevExpress.XtraEditors.XtraForm
    {

        #region [Khai báo biến]
        private DBMain m_DBMain = new DBMain();
        private bds m_bds = new bds();

        private int m_iAction = ACTION_NO;
        private const int ACTION_EDIT = 2;
        private const int ACTION_NEW = 1;
        private const int ACTION_NO = 0;
        private string m_strOldID;
        #endregion

        private string m_Rule = UniMana.Utils.Configs.RuleConfig.TRAININGPRO;
        /// <summary>
        /// Hàm khởi tạo
        /// </summary>
        /// 
        public FrmTrainingProgram()
        {
            if (!SysUtils.CheckAccessByRule(m_Rule))
            {
                return;
            }
            InitializeComponent();
            SysUtils.SetRuleToXucBar(xucBar1, this.m_Rule);
            SysUtils.SetRuleControl(this.Controls, this.Name);

            this.gluCatalogue_MajorF.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");

            this.lueCatalogue_Level.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Levels, "Catalogue_LevelKey", "Name");
            this.lueCatalogue_TrainingType.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes, "Catalogue_TrainingTypeKey", "Name");
            this.lueCatalogue_Course.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Courses, "Catalogue_CourseKey", "Name");
            this.gluCatalogue_Major.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");

            this.rilCatalogue_LevelKey.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Levels, "Catalogue_LevelKey", "Name");
            this.rilCatalogue_MajorKey.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");
            this.rilCatalogue_TrainingTypeKey.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_TrainingTypes, "Catalogue_TrainingTypeKey", "Name");
            this.rilCatalogue_CourseKey.GetData(this.m_DBMain.m_DCReadOnly.Catalogue_Courses, "Catalogue_CourseKey", "Name");

        }

        #region [Sự kiện button]
        private void btnNew_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckAddByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }
            //this.m_bds.AddNew();
            //this.m_iAction = ACTION_NEW;
            //this.Add_Edit();
            xucBar1.dlg.Close();
            new FrmTrainingProgram_SubDetail().ShowDialog();
        }

        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckDeleteByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }
            if (this.m_bds.Count > 0)
            {
                DialogResult dialogResult = this.ShowConfirm();
                if (dialogResult == DialogResult.Yes)
                {
                    int trainingProgramKey = (int)this.grvTraining_TrainingProgram.GetFocusedRowCellValue(gclTraining_TrainingProgramKey);
                    if (this.m_DBMain.m_DCReadOnly.Training_TrainingPrograms.CheckBeforDelete(this.m_DBMain.m_DCReadOnly.Training_TrainingProgram_Details, trainingProgramKey))
                    {
                        this.m_bds.DeleteCurrent();
                        this.Validate();
                        this.Save(this.m_bds, this.m_DBMain.m_DCSave);
                    }
                    else
                    {
                        XtraMessageBox.Show("Không thể xóa dữ liệu này khi còn các dữ liệu liên quan.");
                    }
                }
            }
            else
            {
                XtraMessageBox.Show("Bạn không thể xóa khi không có dữ liệu.");
            }
        }

        private void btnSave_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckSaveByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }
            this.Validate();
            bool bScc = true;
            if (this.m_iAction == ACTION_NEW)
            {
                Training_TrainingProgramInfo trainingProgramInfo = new Training_TrainingProgramInfo(this.txtID.Text);
                if (trainingProgramInfo.Properties.Training_TrainingProgramKey != 0)
                {
                    bScc = false;
                    XtraMessageBox.Show("Mã chương trình đào tạo đã tồn tại, xin vui lòng kiểm tra lại.");
                }
            }
            if (this.m_iAction == ACTION_EDIT)
            {
                if (this.txtID.Text != this.m_strOldID)
                {
                    Training_TrainingProgramInfo trainingProgramInfo = new Training_TrainingProgramInfo(this.txtID.Text);
                    if (trainingProgramInfo.Properties.Training_TrainingProgramKey != 0)
                    {
                        bScc = false;
                        XtraMessageBox.Show("Mã chương trình đào tạo đã tồn tại, xin vui lòng kiểm tra lại.");
                    }
                }
            }
            if (bScc)
            {
                this.m_iAction = ACTION_NO;
                this.Save(this.m_bds, this.m_DBMain.m_DCSave);
                this.Save_Cancel();
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (this.gluCatalogue_MajorF.EditValue != null)
            {
                int majorKey = this.gluCatalogue_MajorF.GetIntValue();
                this.m_bds.DataSource = this.m_DBMain.m_DCSave.Training_TrainingPrograms.GetListByMajorKey(majorKey);
                this.gctTraining_TrainingProgram.DataSource = this.m_bds;
                this.lcgTrainingProgram.LoadOneRecord(this.m_bds);
            }
            else
            {
                XtraMessageBox.Show("Bạn hãy chọn khóa muốn xem");
            }
        }
        private void btnCancel_Click(object sender, ItemClickEventArgs e)
        {
            this.m_iAction = ACTION_NO;
            this.Save_Cancel();
        }
        private void btnEdit_Click(object sender, ItemClickEventArgs e)
        {
            //this.m_iAction = ACTION_EDIT;
            //this.m_strOldID = this.txtID.Text;
            //this.Add_Edit();
        }
        #endregion

        #region [Sự kiện LookupEdit]
        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            GetProgram();
        }
        private void lueCatalogue_Course_EditValueChanged(object sender, EventArgs e)
        {
            GetProgram();
        }

        private void lueCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            GetProgram();
        }

        private void lueCatalogue_TrainingType_EditValueChanged(object sender, EventArgs e)
        {
            GetProgram();
        }

        #endregion


        #region [Sự kiện GridControl]

        #endregion


        #region [Sự kiện textbox]

        #endregion


        #region [Sự kiện khác]
        private void GetProgram()
        {
        }
        #endregion


        #region [Các hàm xử lý]
        private void Add_Edit()
        {
            grcInfo.Enabled = true;
            grcProgram.Enabled = false;
            xucBar1.AddNewVisible = false;
            xucBar1.EditVisible = false;
            xucBar1.DeleteVisible = false;
            xucBar1.CancelVisible = true;
            xucBar1.SaveVisible = true;
            xucBar1.PrintVisible = false;

        }
        private void Save_Cancel()
        {
            grcInfo.Enabled = false;
            grcProgram.Enabled = true;
            xucBar1.AddNewVisible = true;
            xucBar1.EditVisible = true;
            xucBar1.DeleteVisible = true;
            xucBar1.PrintVisible = true;

            xucBar1.CancelVisible = false;
            xucBar1.SaveVisible = false;
        }
        #endregion

        private void gctTraining_TrainingProgram_Click(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// James Viết thêm vào !
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gctTraining_TrainingProgram_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int proKey = int.Parse(grvTraining_TrainingProgram.GetFocusedRowCellValue("Training_TrainingProgramKey").ToString());
                /*FrmTrainingProgram_Detail1 frm = new FrmTrainingProgram_Detail1();*/
                FrmTrainingProgram_SubDetail frm = new FrmTrainingProgram_SubDetail();
                Training_TrainingProgramInfo proInfo = new Training_TrainingProgramInfo(proKey);
                frm.proInfo = proInfo;
                frm.ShowDialog();
            }
            catch /*(Exception ex)*/ { /*MessageBox.Show(ex.Message);*/ }
        }

        private void xucBar_btnEdit_Click(object sender, ItemClickEventArgs e)
        {
            this.xucBar1.dlg.Close();
            using (FrmCopyTrainingProgram frm = new FrmCopyTrainingProgram())
            {
                frm.ShowDialog();
            }
        }
    }
}